package com.ruoyi.wx.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.wx.domain.Region;
import com.ruoyi.wx.service.IRegionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 大区Controller
 * 
 * @author wt
 * @date 2020-12-13
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/wx/region" )
public class RegionController extends BaseController {

    private final IRegionService iRegionService;

    /**
     * 查询大区列表
     */
    @PreAuthorize("@ss.hasPermi('wx:region:list')")
    @GetMapping("/list")
    public TableDataInfo list(Region region)
    {
        startPage();
        LambdaQueryWrapper<Region> lqw = Wrappers.lambdaQuery(region);
        if (StringUtils.isNotBlank(region.getName())){
            lqw.like(Region::getName ,region.getName());
        }
        if (region.getState() != null){
            lqw.eq(Region::getState ,region.getState());
        }
        if (StringUtils.isNotBlank(region.getExtName())){
            lqw.like(Region::getExtName ,region.getExtName());
        }
        List<Region> list = iRegionService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出大区列表
     */
    @PreAuthorize("@ss.hasPermi('wx:region:export')" )
    @Log(title = "大区" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(Region region) {
        LambdaQueryWrapper<Region> lqw = new LambdaQueryWrapper<Region>(region);
        List<Region> list = iRegionService.list(lqw);
        ExcelUtil<Region> util = new ExcelUtil<Region>(Region. class);
        return util.exportExcel(list, "region" );
    }

    /**
     * 获取大区详细信息
     */
    @PreAuthorize("@ss.hasPermi('wx:region:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iRegionService.getById(id));
    }

    /**
     * 新增大区
     */
    @PreAuthorize("@ss.hasPermi('wx:region:add')" )
    @Log(title = "大区" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Region region) {
        return toAjax(iRegionService.save(region) ? 1 : 0);
    }

    /**
     * 修改大区
     */
    @PreAuthorize("@ss.hasPermi('wx:region:edit')" )
    @Log(title = "大区" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Region region) {
        return toAjax(iRegionService.updateById(region) ? 1 : 0);
    }

    /**
     * 删除大区
     */
    @PreAuthorize("@ss.hasPermi('wx:region:remove')" )
    @Log(title = "大区" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iRegionService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
